package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Trend;
import com.entity.TrendUserProject;
import com.entity.User;
import com.util.DBHelp;
import com.util.PageHelp;
import com.util.RowMapper;

public class TrendUserProjectDao {

	private DBHelp<TrendUserProject> db=new DBHelp<TrendUserProject>();
	
	/*public PageHelp<TrendUserProject> findByUserId(PageHelp<TrendUserProject> page,String userId){
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT tup.trendid,tup.userid,tup.projectid,tt.content,tt.createtime,tt.user.username,tu.image FROM t_trend_user_project AS tup ");
		sql.append("LEFT JOIN t_user AS tu ON tup.userid = tu.id ");
		sql.append("LEFT JOIN t_trend AS tt ON tup.trendid = tt.id ");
		sql.append("where tup.userid = ? ");
		sql.append("order by tt.createtime desc limit ?,?");
		List<TrendUserProject> list =db.executeQueryForList(sql.toString(), new TUPRowMapper(),userId,page.getOffSet(),page.getPageSize());
		page.setResult(list);
		return page;
	}*/
	
	private class TUPRowMapper implements RowMapper<TrendUserProject>{

		public TrendUserProject mapRow(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			TrendUserProject tup=new TrendUserProject();
			
			
			/*User u=new User();
			u.setId(rs.getString("userid"));
			u.setImage(rs.getString("image"));
			u.setUserName(rs.getString("username"));
			tup.setUser(u);*/
			tup.setUserid(rs.getString("userid"));
			
			
			/*Trend t=new Trend();
			t.setContent(rs.getString("content"));
			t.setCreateTime(rs.getString("createtime"));
			t.setId(rs.getString("trendid"));
			tup.setTrend(t);*/
			tup.setTrendid(rs.getString("trendid"));
			
			tup.setProjectid(rs.getString("projectid"));
			return tup;
		}
		
	}


	public void save(TrendUserProject tup){
		// TODO Auto-generated method stub
		String sql="insert into t_trend_user_project(trendid,userid,projectid) values(?,?,?)";
		db.executeSQL(sql, tup.getTrendid(),tup.getUserid(),tup.getProjectid());
	}


	public Long getCount() {
		String sql="select count(trendid) from t_trend_user_project";
		return (Long) db.executeQueryForFunction(sql);
	}


	public List<TrendUserProject> findById(String userid) {
		// TODO Auto-generated method stub
		String sql="select trendid,userid,projectid from t_trend_user_project where userid=?";
		return db.executeQueryForList(sql, new TUPRowMapper(),userid);
	}
	
}
